# 紧急修复脚本 - 服务端稳定性问题
Write-Host "🚨 开始执行服务端稳定性紧急修复..." -ForegroundColor Red

# 1. 检查服务状态
Write-Host "📊 检查服务状态..." -ForegroundColor Yellow
$serviceName = "Lzfy_Refund_Service"  # 请根据实际服务名称调整
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if ($service) {
    Write-Host "✅ 找到服务: $serviceName, 状态: $($service.Status)" -ForegroundColor Green
} else {
    Write-Host "⚠️ 未找到服务: $serviceName" -ForegroundColor Yellow
}

# 2. 清理Oracle连接池
Write-Host "🧹 清理Oracle连接池..." -ForegroundColor Yellow
try {
    $oracleDll = Get-ChildItem -Path ".\*Oracle.ManagedDataAccess*" -Recurse | Select-Object -First 1
    if ($oracleDll) {
        Add-Type -Path $oracleDll.FullName
        [Oracle.ManagedDataAccess.Client.OracleConnection]::ClearAllPools()
        Write-Host "✅ Oracle连接池已清理" -ForegroundColor Green
    } else {
        Write-Host "⚠️ 未找到Oracle.ManagedDataAccess程序集" -ForegroundColor Yellow
    }
} catch {
    Write-Host "⚠️ 清理连接池时出错: $($_.Exception.Message)" -ForegroundColor Yellow
}

# 3. 重启服务（如果存在）
if ($service -and $service.Status -eq "Running") {
    Write-Host "🔄 重启服务..." -ForegroundColor Yellow
    try {
        Restart-Service -Name $serviceName -Force
        Start-Sleep -Seconds 5
        $service.Refresh()
        Write-Host "✅ 服务重启完成，当前状态: $($service.Status)" -ForegroundColor Green
    } catch {
        Write-Host "❌ 服务重启失败: $($_.Exception.Message)" -ForegroundColor Red
    }
}

# 4. 验证修复效果
Write-Host "🔍 验证修复效果..." -ForegroundColor Yellow
Start-Sleep -Seconds 3

# 5. 提供监控URL
Write-Host "📊 监控URL:" -ForegroundColor Cyan
Write-Host "  - 数据库连接池状态: http://localhost:5000/api/health/database-pool-status" -ForegroundColor Cyan
Write-Host "  - 应用健康检查: http://localhost:5000/api/health" -ForegroundColor Cyan
Write-Host "  - 分页查询测试: POST http://localhost:5000/api/RefundTaskMaster/get-all-paged" -ForegroundColor Cyan

Write-Host "🎯 紧急修复完成！" -ForegroundColor Green